<?php
/**
 * 升级程序
 * @author 65736
 *
 */
namespace app\crontab\controller;

use app\common\model\article\HeatFactorModel;
use app\common\model\article\PublishArticleModel;
use app\common\model\article\PublishWeiboModel;
use app\common\model\client\H5StaticVersionModel;
use app\common\model\coin\CoinRuntimeModel;
use app\common\model\pair\PairRuntimeModel;
use app\common\model\spider\SpiderArticleModel;
use app\common\model\spider\SpiderDetailModel;
use app\common\redis\ArticlePublicRedis;
use app\common\redis\CoinArticleRedis;

class Update extends CrontabCommon {
	/**
	 * 初始化方法
	 */
	protected function _initialize(){
		//先执行父类初始化
		parent::_initialize();
	}

    /**
     * 生成正式币种日报文章
     * @return bool
     */
	public function create_daily_article() {
		// 获取币种
		$modelCoin = new CoinRuntimeModel();
		$where = [
				'status' => 1,
				'type' => 1,
				'sort' => array('gt', 0),
				'deleted' => -1,
		];
		$coin_id_list = $modelCoin->search_id_list( $where, $page = 1, $asc = true, $sort_by = 'sort', $list_rows = 200);
		if ( !$coin_id_list ) {
			echo "没有可用币种".PHP_EOL;
			return false;
		}
		// 文章模型
		foreach ( $coin_id_list as $_coin_id ) {
			$modelArticle = new PublishArticleModel();
			// 设置成不验证
			$res = $modelArticle->createDataDaily($_coin_id, false, $is_data_daily=false, $is_list=false);
			if ( !$res ) {
				echo $modelArticle->getError().PHP_EOL;
			}
		}
		return true;
	}
	
	/**
	 * 执行h5静态资源打包并上传
	 */
	public function run_h5_static_zip() {
		$model = new H5StaticVersionModel();
		$zipRes = $model->zipSource();
		if ( !$zipRes ) {
			echo $model->getError().PHP_EOL;
			return false; 
		}
		$res = $model->saveOss();
		$version = (isset($_GET['version'])&& $_GET['version'] ) ?  $_GET['version'] : time();
		$url = $res;
		$insert = array(
				'version' => $version,
				'url' => $url,
		);
		
		return $model->add_data($insert);
	}
	
	/**
	 * 计算抓取文章综合平均分
	 * 
	 */
	public function calculate_spider_article_score() {
		$modelSpiderArticle = new SpiderArticleModel();
		$id_list = $modelSpiderArticle->search_id_list_no_page();
		if ($id_list) {
			$data_list = $modelSpiderArticle->muti_get_data_by_id_list($id_list);
			// 计算总数量
			$total = count($data_list);
			// 10 个进程
			$process = 10;
			// 每个进程数据数量
			$limit = ceil($total/$process);
			// 数据切割分组
			$all_data_chunk = array_chunk($data_list, $limit);
			// 主进程池
			$pid_list = array();
			// 多进程处理
			foreach ( $all_data_chunk as $kp=>$vp ) {
				$pid = pcntl_fork();
				if( $pid==-1 ){
					die("can not fork");
				}elseif ( $pid ) {
					$pid_list[] = $pid;
					//echo "I am the parent $kp\n";
				}else {
					// 执行代码
					//echo "I am the child $kp\n";
					$modelSpiderArticle = new SpiderArticleModel();
					$modelHeatFactor =  new HeatFactorModel();
					foreach ( $vp as $v ) {
						$hots = $v['hots'];
						if ($hots) {
							$score = $modelHeatFactor->calculate_avg_heat_factor($hots);
							$update = array(
									"avg_score" => $score
							);
							$modelSpiderArticle->update_data($v['id'], $update);
						}
					}
					exit();
				}
			}// End chunk foreach
			while(count($pid_list) > 0)
			{
				$myId = pcntl_waitpid(-1, $status, WNOHANG);
				foreach($pid_list as $key => $pid)
				{
					if($myId == $pid) unset($pid_list[$key]);
				}
			}		
		}
		
	}
	
	/**
	 * 更新抓取文章原文到过滤后的详情
	 */
	public function update_spider_detail() {
		$modelSpiderDetail = new SpiderDetailModel();
		$map = array(
				'detail' => ''
		);
		$id_list = $modelSpiderDetail->search_id_list($map, 1, false, 'id', 5000);
		if ($id_list) {
			$detail_data_list = $modelSpiderDetail->muti_get_data_by_id_list($id_list);
			// 计算总数量
			$total = count($detail_data_list);
			// 10 个进程
			$process = 10;
			// 每个进程数据数量
			$limit = ceil($total/$process);
			// 数据切割分组
			$all_data_chunk = array_chunk($detail_data_list, $limit);
			// 主进程池
			$pid_list = array();
			// 多进程处理
			foreach ( $all_data_chunk as $kp=>$vp ) {
				$pid = pcntl_fork();
				if( $pid==-1 ){
					die("can not fork");
				}elseif ( $pid ) {
					$pid_list[] = $pid;
					//echo "I am the parent $kp\n";
				}else {
					// 执行代码
					//echo "I am the child $kp\n";
					$modelSpiderArticle = new SpiderArticleModel();
					$modelDetail = new SpiderDetailModel();
					foreach ( $vp as $v ) {
						$id = $v['id'];
						$content = $v['content'];
						$artData = $modelSpiderArticle->get_data_by_id($id);
						$detail = $modelDetail->filter_spider_content($artData['spider_from'], $content);
						$update = array(
								'detail' => $detail
						);
						$modelDetail->update_data($id, $update);
					}
					exit();
				}
			}// End chunk foreach
			while(count($pid_list) > 0)
			{
				$myId = pcntl_waitpid(-1, $status, WNOHANG);
				foreach($pid_list as $key => $pid)
				{
					if($myId == $pid) unset($pid_list[$key]);
				}
			}
		}
	}
	
	/**
	 * 更新币种相关文章数据Redis
	 */
	public function update_coin_article() {
		$modelCoin = new CoinRuntimeModel();
		$coin_id_list = $modelCoin->search_id_list_no_page(array());
		
		if ( $coin_id_list ) {
			// 计算总数量
			$total = count($coin_id_list);
			// 10 个进程
			$process = 10;
			// 每个进程数据数量
			$limit = ceil($total/$process);
			// 数据切割分组
			$all_data_chunk = array_chunk($coin_id_list, $limit);
			// 主进程池
			$pid_list = array();
			// 多进程处理
			foreach ( $all_data_chunk as $kp=>$vp ) {
				$pid = pcntl_fork();
				if( $pid==-1 ){
					die("can not fork");
				}elseif ( $pid ) {
					$pid_list[] = $pid;
					//echo "I am the parent $kp\n";
				}else {
					// 执行代码
					//echo "I am the child $kp\n";
					$redisCoinArticle = new CoinArticleRedis();
					foreach ( $vp as $coin_id ) {
						$redisCoinArticle->createCoinArticleList($coin_id);
					}
					exit();
				}
			}// End chunk foreach
			while(count($pid_list) > 0)
			{
				$myId = pcntl_waitpid(-1, $status, WNOHANG);
				foreach($pid_list as $key => $pid)
				{
					if($myId == $pid) unset($pid_list[$key]);
				}
			}
		}
	}
	
	/**
	 * 更新币种微博数据Redis
	 */
	public function update_coin_weibo() {
		$modelCoin = new CoinRuntimeModel();
		$coin_id_list = $modelCoin->search_id_list_no_page(array());
		
		if ( $coin_id_list ) {
			// 计算总数量
			$total = count($coin_id_list);
			// 10 个进程
			$process = 10;
			// 每个进程数据数量
			$limit = ceil($total/$process);
			// 数据切割分组
			$all_data_chunk = array_chunk($coin_id_list, $limit);
			// 主进程池
			$pid_list = array();
			// 多进程处理
			foreach ( $all_data_chunk as $kp=>$vp ) {
				$pid = pcntl_fork();
				if( $pid==-1 ){
					die("can not fork");
				}elseif ( $pid ) {
					$pid_list[] = $pid;
					//echo "I am the parent $kp\n";
				}else {
					// 执行代码
					//echo "I am the child $kp\n";
					$redisCoinWeibo = new CoinArticleRedis();
					foreach ( $vp as $coin_id ) {
						$redisCoinWeibo->createCoinWeiboList($coin_id);
					}
					exit();
				}
			}// End chunk foreach
			while(count($pid_list) > 0)
			{
				$myId = pcntl_waitpid(-1, $status, WNOHANG);
				foreach($pid_list as $key => $pid)
				{
					if($myId == $pid) unset($pid_list[$key]);
				}
			}
		}
	}
	
	/**
	 * 创建非币相关所有类型文章集合 (sort set)
	 */
	public function update_nocoin_allTypeArticle() {
		$redisCoin = new CoinArticleRedis();
		$redisCoin->CreateNoCoinAllTypeArticle();
	}
	
	/**
	 * 创建币相关所有类型文章集合 (sort set)
	 */
	public function update_coin_allTypeArticle() {
		$modelCoin = new CoinRuntimeModel();
		$coin_id_list = $modelCoin->search_id_list_no_page(array());
		
		if ( $coin_id_list ) {
			// 计算总数量
			$total = count($coin_id_list);
			// 10 个进程
			$process = 10;
			// 每个进程数据数量
			$limit = ceil($total/$process);
			// 数据切割分组
			$all_data_chunk = array_chunk($coin_id_list, $limit);
			// 主进程池
			$pid_list = array();
			// 多进程处理
			foreach ( $all_data_chunk as $kp=>$vp ) {
				$pid = pcntl_fork();
				if( $pid==-1 ){
					die("can not fork");
				}elseif ( $pid ) {
					$pid_list[] = $pid;
					//echo "I am the parent $kp\n";
				}else {
					// 执行代码
					//echo "I am the child $kp\n";
					$redisCoin = new CoinArticleRedis();
					foreach ( $vp as $coin_id ) {
						$redisCoin->CreateCoinAllTypeArticle($coin_id);
					}
					exit();
				}
			}// End chunk foreach
			while(count($pid_list) > 0)
			{
				$myId = pcntl_waitpid(-1, $status, WNOHANG);
				foreach($pid_list as $key => $pid)
				{
					if($myId == $pid) unset($pid_list[$key]);
				}
			}
		}
	}
	
	/**
	 * 更新微博相关文章
	 */
	public function update_weibo_relate() {
		$modelWeibo = new PublishWeiboModel();
		$weiboMap = array(
				'status' => 3, // 已通过
				'publish_status' => 2, //已发布
		);
		$weibo_id_list = $modelWeibo->search_id_list( $weiboMap, 1, false, 'create_time', 1000 );
		//$weibo_id_list = $modelWeibo->search_id_list( $weiboMap, 1, false, 'create_time');
		Log::write("weibo_id_list:".count($weibo_id_list));
		if ( $weibo_id_list ) {
			// 计算总数量
			$total = count($weibo_id_list);
			// 10 个进程
			$process = 10;
			// 每个进程数据数量
			$limit = ceil($total/$process);
			// 数据切割分组
			$all_data_chunk = array_chunk($weibo_id_list, $limit);
			// 主进程池
			$pid_list = array();
			// 多进程处理
			foreach ( $all_data_chunk as $kp=>$vp ) {
				$pid = pcntl_fork();
				if( $pid==-1 ){
					die("can not fork");
				}elseif ( $pid ) {
					$pid_list[] = $pid;
					//echo "I am the parent $kp\n";
				}else {
					// 执行代码
					//echo "I am the child $kp\n";
					$articleRedis = new ArticlePublicRedis();
					foreach ( $vp as $weibo_id ) {
						$articleRedis->CreateWeiboRelateAW($weibo_id);
					}
					exit();
				}
			}// End chunk foreach
			while(count($pid_list) > 0)
			{
				$myId = pcntl_waitpid(-1, $status, WNOHANG);
				foreach($pid_list as $key => $pid)
				{
					if($myId == $pid) unset($pid_list[$key]);
				}
			}
		}
	}
	
	/**
	 * 更新文章相关文章
	 */
	public function update_article_relate() {
		$modelArticle = new PublishArticleModel();
		$articleMap = array(
				'is_published' => 1,
				'status' => 1,
				'deleted' => -1,
		);
		$article_id_list = $modelArticle->search_id_list( $articleMap, 1, false, 'create_time', 10000 );
		//$article_id_list = $modelArticle->search_id_list_no_page(  $articleMap, 'id', false, 'create_time' );
		Log::write("article_id_list:".count($article_id_list));
		if ( $article_id_list ) {
			// 计算总数量
			$total = count($article_id_list);
			// 10 个进程
			$process = 10;
			// 每个进程数据数量
			$limit = ceil($total/$process);
			// 数据切割分组
			$all_data_chunk = array_chunk($article_id_list, $limit);
			// 主进程池
			$pid_list = array();
			// 多进程处理
			foreach ( $all_data_chunk as $kp=>$vp ) {
				$pid = pcntl_fork();
				if( $pid==-1 ){
					die("can not fork");
				}elseif ( $pid ) {
					$pid_list[] = $pid;
					//echo "I am the parent $kp\n";
				}else {
					// 执行代码
					//echo "I am the child $kp\n";
					$articleRedis = new ArticlePublicRedis();
					foreach ( $vp as $article_id ) {
						$articleRedis->CreateArticleRelateAW($article_id);
					}
					exit();
				}
			}// End chunk foreach
			while(count($pid_list) > 0)
			{
				$myId = pcntl_waitpid(-1, $status, WNOHANG);
				foreach($pid_list as $key => $pid)
				{
					if($myId == $pid) unset($pid_list[$key]);
				}
			}
		}
	}
	
	/**
	 * 生成(自选)币列表价格曲线图片
	 * @version PM 高松 20180630 修改行情折线图策略 改为1天数据 24个点
	 */
	public function GenCoinListProfileImage() {
		require_cache( WEB_LIB."Pchart.class.php" );
		$pchart = new Pchart();
		// 获取所有币列表
		$modelCoin = new CoinRuntimeModel();
		$symbol_list = $modelCoin->get_key_value("id", "symbol");
		if ( $symbol_list ) {
			$file_path = WEB_UPLOADS.'oss_tmp/'.CoinRuntimeModel::coinListProfileImageOssPath;
			if ( !is_dir($file_path) ) {
				@mkdir($file_path, 0777, true);
			}
			$storage = new Storage();
			foreach ( $symbol_list as $coin_id=>$symbol ) {
				// 图片文件绝对路径
				$file_path_name = $file_path.$coin_id.$symbol.".png";
				// Oss 路径
				$objectKey = CoinRuntimeModel::coinListProfileImageOssPath.$coin_id.$symbol.".png";
				
				// @todo 数据 临时取USD, 14天
				$zb_list = $modelCoin->get_zb_global_charts_data_list($coin_id, 1);
				if ( !$zb_list ) {
					//echo "{$v} No candles!!".PHP_EOL;
					continue;
				}
				
				// X轴数据
				$x_data = array();
				// Y轴数据
				$y_data = array();
				
				// 按天归类
				$dateList = array();
				$timeList = array();
				foreach ( $zb_list as $v ) {
					list( $timestemp, $btc, $usd, $volume ) = $v;
					$time = $timestemp/1000;
// 					$date = date("Ymd", $time);
// 					$second = $time-strtotime($date);
// 					$dateList[$date][$second] = $usd;
					$dateList[$time] = $usd;
					$timeList[] = $time;
				}
				
				// 获取每组数据最大值赋值给曲线图数据
				// 				foreach ( $dateList as $date =>$v ) {
				// 					$y_data[] = $v[ max( array_keys($v) ) ];
				// 					$x_data[] = $date;
				// 				}
				
				
				// 总计需要 48 个点
				$point = 48;
				$list_count = count($timeList);
				$ceil_group = ceil($list_count/$point);
				$timeGroup = array_chunk($timeList, $ceil_group);
				foreach ( $timeGroup as $_gt ) {
					$_kt = min( $_gt );
					$y_data[] = $dateList[$_kt];
					$x_data[] = $_kt;
				}
				
				
				$res = $pchart->GenCoinListProfileImage($x_data, $y_data, $file_path_name, 480, 240);
				if ( $res ) {
					$storage->save_resource($objectKey, $file_path_name);
					@unlink($file_path_name);
				}else {
					echo "Error {$file_path_name}".PHP_EOL;
				}
			} // End Foreach
		} // End IF
	}
	
	/**
	 * 通过张斌的交易对列表同步数据到pair_runtime
	 */
	public function sync_data_from_zb_pair_list() {
		$modelPair = new PairRuntimeModel();
		$insert_list = $modelPair->sync_pair_data_from_zb_ticker_list();
		if ( $insert_list ) {
			// 计算总数量
			$total = count($insert_list);
			// 5 个进程
			$process = 5;
			// 每个进程数据数量
			$limit = ceil($total/$process);
			// 数据切割分组
			$all_data_chunk = array_chunk($insert_list, $limit);
			// 主进程池
			$pid_list = array();
			// 多进程处理
			foreach ( $all_data_chunk as $kp=>$vp ) {
				$pid = pcntl_fork();
				if( $pid==-1 ){
					die("can not fork");
				}elseif ( $pid ) {
					$pid_list[] = $pid;
				}else {
					// 执行代码
					$modelPair = new PairRuntimeModel();
					// 循环单元结构, 每个单元是一个币种数据
					foreach ( $vp as $v ) {
						$insert_res = $modelPair->add_data($v);
						if ( !$insert_res ) {
							//echo "Error ".$v['exchange']."/".$v['symbol']."/".$v['pair_name'].PHP_EOL;
							//echo $modelPair->getLastSql().PHP_EOL;
							
							$error =  "Error ".$v['exchange']."/".$v['symbol']."/".$v['pair_name'];
							$sql = $modelPair->getLastSql();
							
							Log::write($error);
							Log::write($sql);
						}
					}
					exit();
				}
			} // End chunk foreach
			while(count($pid_list) > 0)
			{
				$myId = pcntl_waitpid(-1, $status, WNOHANG);
				foreach($pid_list as $key => $pid)
				{
					if($myId == $pid) unset($pid_list[$key]);
				}
			}
		} // End IF
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
?>